package com.s7.compat.adapter;

import android.widget.RadioGroup;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.fragment.app.FragmentTransaction;

import com.s7.compat.listener.OnRadioTabListener;

import java.util.List;


public class FragmentTabAdapter implements RadioGroup.OnCheckedChangeListener {

    private List<Fragment> fragments;
    private RadioGroup rgs;
    private FragmentActivity fActivity;
    private int fragmentContentId;
    private int currentTab;
    private OnRadioTabListener mListener;

    public FragmentTabAdapter(FragmentActivity fActivity, List<Fragment> fragments, int fragmentContentId, RadioGroup rgs) {
        init(fActivity, fragments, 0, fragmentContentId, rgs);

    }

    public FragmentTabAdapter(FragmentActivity fActivity, List<Fragment> fragments, int index, int fragmentContentId, RadioGroup rgs) {
        init(fActivity, fragments, index, fragmentContentId, rgs);

    }

    private void init(FragmentActivity fActivity, List<Fragment> fragments, int index, int fragmentContentId, RadioGroup rgs) {
        this.fActivity = fActivity;
        this.fragments = fragments;
        this.rgs = rgs;
        this.fragmentContentId = fragmentContentId;
        FragmentTransaction ft = fActivity.getSupportFragmentManager().beginTransaction();
        ft.add(fragmentContentId, fragments.get(index));
        ft.commit();
        rgs.setOnCheckedChangeListener(this);
    }

    @Override
    public void onCheckedChanged(RadioGroup radioGroup, int checkedId) {
        for(int i = 0; i < rgs.getChildCount(); i++){
            if(rgs.getChildAt(i).getId() == checkedId){
                Fragment fragment = fragments.get(i);
                FragmentTransaction ft = obtainFragmentTransaction();

                getCurrentFragment().onPause();

                if(fragment.isAdded()){
                    fragment.onResume();
                }else{
                    ft.add(fragmentContentId, fragment);
                }
                showTab(i);
                ft.commit();
                if(null != mListener){
                    mListener.onRgsTab(radioGroup, checkedId);
                }
            }
        }
    }

    private void showTab(int index){
        for(int i = 0; i < fragments.size(); i++){
            Fragment fragment = fragments.get(i);
            FragmentTransaction ft = obtainFragmentTransaction();
            if(index == i){
                ft.show(fragment);
            }else{
                ft.hide(fragment);
            }
            ft.commit();
        }
        currentTab = index;
    }

    private FragmentTransaction obtainFragmentTransaction(){
        FragmentTransaction ft = fActivity.getSupportFragmentManager().beginTransaction();
        return ft;
    }


    public int getCurrentTab() {
        return currentTab;
    }

    public Fragment getCurrentFragment(){
        return fragments.get(currentTab);
    }

    public void onShowTab(int index) {
        for(int i = 0; i < rgs.getChildCount(); i++){
            if (i == index) {
                rgs.check(rgs.getChildAt(i).getId());
                Fragment fragment = fragments.get(i);
                FragmentTransaction ft = obtainFragmentTransaction();
                getCurrentFragment().onPause();
                if(fragment.isAdded()){
                    fragment.onResume();
                }else{
                    ft.add(fragmentContentId, fragment);
                }
                showTab(i);
                ft.commit();
            }
        }
    }

    public OnRadioTabListener getOnRgsTabListener() {
        return mListener;
    }

    public void setOnRgsTabListener(OnRadioTabListener mListener) {
        this.mListener = mListener;
    }



}
